/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import javolution.util.FastList;

import l2s.commons.util.Rnd;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.scripting.ScriptFile;

public class Q233_TestOfWarspirit extends Quest implements ScriptFile
{
	private static final String qn = "Q233_TestOfWarspirit";
	private static final int Somak = 30510;
	private static final int Vivyan = 30030;
	private static final int Sarien = 30436;
	private static final int Racoy = 30507;
	private static final int Manakia = 30515;
	private static final int Orim = 30630;
	private static final int Ancestor_Martankus = 30649;
	private static final int Pekiron = 30682;
	private static final int Porta = 20213;
	private static final int Excuro = 20214;
	private static final int Mordeo = 20215;
	private static final int Noble_Ant = 20089;
	private static final int Noble_Ant_Leader = 20090;
	private static final int Leto_Lizardman_Shaman = 20581;
	private static final int Leto_Lizardman_Overlord = 20582;
	private static final int Medusa = 20158;
	private static final int Stenoa_Gorgon_Queen = 27108;
	private static final int Tamlin_Orc = 20601;
	private static final int Tamlin_Orc_Archer = 20602;
	private static final short Dimensional_Diamond = 7562;
	private static final short MARK_OF_WARSPIRIT = 2879;
	private static final short VENDETTA_TOTEM = 2880;
	private static final short TAMLIN_ORC_HEAD = 2881;
	private static final short WARSPIRIT_TOTEM = 2882;
	private static final short ORIMS_CONTRACT = 2883;
	private static final short PORTAS_EYE = 2884;
	private static final short EXCUROS_SCALE = 2885;
	private static final short MORDEOS_TALON = 2886;
	private static final short BRAKIS_REMAINS1 = 2887;
	private static final short PEKIRONS_TOTEM = 2888;
	private static final short TONARS_SKULL = 2889;
	private static final short TONARS_RIB_BONE = 2890;
	private static final short TONARS_SPINE = 2891;
	private static final short TONARS_ARM_BONE = 2892;
	private static final short TONARS_THIGH_BONE = 2893;
	private static final short TONARS_REMAINS1 = 2894;
	private static final short MANAKIAS_TOTEM = 2895;
	private static final short HERMODTS_SKULL = 2896;
	private static final short HERMODTS_RIB_BONE = 2897;
	private static final short HERMODTS_SPINE = 2898;
	private static final short HERMODTS_ARM_BONE = 2899;
	private static final short HERMODTS_THIGH_BONE = 2900;
	private static final short HERMODTS_REMAINS1 = 2901;
	private static final short RACOYS_TOTEM = 2902;
	private static final short VIVIANTES_LETTER = 2903;
	private static final short INSECT_DIAGRAM_BOOK = 2904;
	private static final short KIRUNAS_SKULL = 2905;
	private static final short KIRUNAS_RIB_BONE = 2906;
	private static final short KIRUNAS_SPINE = 2907;
	private static final short KIRUNAS_ARM_BONE = 2908;
	private static final short KIRUNAS_THIGH_BONE = 2909;
	private static final short KIRUNAS_REMAINS1 = 2910;
	private static final short BRAKIS_REMAINS2 = 2911;
	private static final short TONARS_REMAINS2 = 2912;
	private static final short HERMODTS_REMAINS2 = 2913;
	private static final short KIRUNAS_REMAINS2 = 2914;
	private static final short[] Noble_Ant_Drops =
	{
		KIRUNAS_THIGH_BONE,
		KIRUNAS_ARM_BONE,
		KIRUNAS_SPINE,
		KIRUNAS_RIB_BONE,
		KIRUNAS_SKULL
	};
	private static short[] Leto_Lizardman_Drops =
	{
		TONARS_SKULL,
		TONARS_RIB_BONE,
		TONARS_SPINE,
		TONARS_ARM_BONE,
		TONARS_THIGH_BONE
	};
	private static short[] Medusa_Drops =
	{
		HERMODTS_RIB_BONE,
		HERMODTS_SPINE,
		HERMODTS_THIGH_BONE,
		HERMODTS_ARM_BONE
	};
	
	public Q233_TestOfWarspirit(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(Somak);
		addTalkId(Vivyan);
		addTalkId(Sarien);
		addTalkId(Racoy);
		addTalkId(Manakia);
		addTalkId(Orim);
		addTalkId(Ancestor_Martankus);
		addTalkId(Pekiron);
		addKillId(Porta);
		addKillId(Excuro);
		addKillId(Mordeo);
		addKillId(Noble_Ant);
		addKillId(Noble_Ant_Leader);
		addKillId(Leto_Lizardman_Shaman);
		addKillId(Leto_Lizardman_Overlord);
		addKillId(Medusa);
		addKillId(Stenoa_Gorgon_Queen);
		addKillId(Tamlin_Orc);
		addKillId(Tamlin_Orc_Archer);
		questItemIds = new int[]
		{
			VENDETTA_TOTEM,
			TAMLIN_ORC_HEAD,
			WARSPIRIT_TOTEM,
			ORIMS_CONTRACT,
			PORTAS_EYE,
			EXCUROS_SCALE,
			MORDEOS_TALON,
			BRAKIS_REMAINS1,
			PEKIRONS_TOTEM,
			TONARS_SKULL,
			TONARS_RIB_BONE,
			TONARS_SPINE,
			TONARS_ARM_BONE,
			TONARS_THIGH_BONE,
			TONARS_REMAINS1,
			MANAKIAS_TOTEM,
			HERMODTS_SKULL,
			HERMODTS_RIB_BONE,
			HERMODTS_SPINE,
			HERMODTS_ARM_BONE,
			HERMODTS_THIGH_BONE,
			HERMODTS_REMAINS1,
			RACOYS_TOTEM,
			VIVIANTES_LETTER,
			INSECT_DIAGRAM_BOOK,
			KIRUNAS_SKULL,
			KIRUNAS_RIB_BONE,
			KIRUNAS_SPINE,
			KIRUNAS_ARM_BONE,
			KIRUNAS_THIGH_BONE,
			KIRUNAS_REMAINS1,
			BRAKIS_REMAINS2,
			TONARS_REMAINS2,
			HERMODTS_REMAINS2,
			KIRUNAS_REMAINS2
		};
	}
	
	@Override
	public String onEvent(String event, QuestState st)
	{
		byte _state = st.getState();
		if (event.equalsIgnoreCase("30510-05.htm") && (_state == STATE_CREATED))
		{
			st.giveItems(Dimensional_Diamond, 64);
			st.setState(STATE_STARTED);
			st.set("cond", "1");
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		else if (event.equalsIgnoreCase("30630-04.htm") && (_state == STATE_STARTED))
		{
			st.giveItems(ORIMS_CONTRACT, 1);
		}
		else if (event.equalsIgnoreCase("30682-02.htm") && (_state == STATE_STARTED))
		{
			st.giveItems(PEKIRONS_TOTEM, 1);
		}
		else if (event.equalsIgnoreCase("30515-02.htm") && (_state == STATE_STARTED))
		{
			st.giveItems(MANAKIAS_TOTEM, 1);
		}
		else if (event.equalsIgnoreCase("30507-02.htm") && (_state == STATE_STARTED))
		{
			st.giveItems(RACOYS_TOTEM, 1);
		}
		else if (event.equalsIgnoreCase("30030-04.htm") && (_state == STATE_STARTED))
		{
			st.giveItems(VIVIANTES_LETTER, 1);
		}
		else if (event.equalsIgnoreCase("30649-03.htm") && (_state == STATE_STARTED) && (st.getQuestItemsCount(WARSPIRIT_TOTEM) > 0))
		{
			st.takeItems(WARSPIRIT_TOTEM, -1);
			st.takeItems(BRAKIS_REMAINS2, -1);
			st.takeItems(HERMODTS_REMAINS2, -1);
			st.takeItems(KIRUNAS_REMAINS2, -1);
			st.takeItems(TAMLIN_ORC_HEAD, -1);
			st.takeItems(TONARS_REMAINS2, -1);
			st.giveItems(MARK_OF_WARSPIRIT, 1);
			st.getPlayer().addExpAndSp(63483, 17500);
			st.playSound(QuestState.SOUND_FINISH);
			st.unset("cond");
			st.exitQuest(true);
		}
		return event;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(qn);
		if (st.getQuestItemsCount(MARK_OF_WARSPIRIT) > 0)
		{
			st.exitQuest(true);
			return getAlreadyCompletedMsg();
		}
		byte _state = st.getState();
		int npcId = npc.getNpcId();
		if (_state == STATE_CREATED)
		{
			if (npcId != Somak)
			{
				return getNoQuestMsg();
			}
			if (st.getPlayer().getRace().ordinal() != 3)
			{
				st.exitQuest(true);
				return "30510-01.htm";
			}
			if (st.getPlayer().getClassId().getId() != 0x32)
			{
				st.exitQuest(true);
				return "30510-02.htm";
			}
			if (st.getPlayer().getLevel() < 39)
			{
				st.exitQuest(true);
				return "30510-03.htm";
			}
			st.set("cond", "0");
			return "30510-04.htm";
		}
		if ((_state != STATE_STARTED) || (st.getInt("cond") != 1))
		{
			return "noquest";
		}
		if (npcId == Somak)
		{
			if (st.getQuestItemsCount(VENDETTA_TOTEM) > 0)
			{
				if (st.getQuestItemsCount(TAMLIN_ORC_HEAD) < 13)
				{
					return "30510-08.htm";
				}
				st.takeItems(VENDETTA_TOTEM, -1);
				st.giveItems(WARSPIRIT_TOTEM, 1);
				st.giveItems(BRAKIS_REMAINS2, 1);
				st.giveItems(HERMODTS_REMAINS2, 1);
				st.giveItems(KIRUNAS_REMAINS2, 1);
				st.giveItems(TONARS_REMAINS2, 1);
				st.playSound(QuestState.SOUND_MIDDLE);
				return "30510-09.htm";
			}
			if (st.getQuestItemsCount(WARSPIRIT_TOTEM) > 0)
			{
				return "30510-10.htm";
			}
			if ((st.getQuestItemsCount(BRAKIS_REMAINS1) == 0) || (st.getQuestItemsCount(HERMODTS_REMAINS1) == 0) || (st.getQuestItemsCount(KIRUNAS_REMAINS1) == 0) || (st.getQuestItemsCount(TONARS_REMAINS1) == 0))
			{
				return "30510-06.htm";
			}
			st.takeItems(BRAKIS_REMAINS1, -1);
			st.takeItems(HERMODTS_REMAINS1, -1);
			st.takeItems(KIRUNAS_REMAINS1, -1);
			st.takeItems(TONARS_REMAINS1, -1);
			st.giveItems(VENDETTA_TOTEM, 1);
			st.playSound(QuestState.SOUND_MIDDLE);
			return "30510-07.htm";
		}
		if (npcId == Orim)
		{
			if (st.getQuestItemsCount(ORIMS_CONTRACT) > 0)
			{
				if ((st.getQuestItemsCount(PORTAS_EYE) < 10) || (st.getQuestItemsCount(EXCUROS_SCALE) < 10) || (st.getQuestItemsCount(MORDEOS_TALON) < 10))
				{
					return "30630-05.htm";
				}
				st.takeItems(ORIMS_CONTRACT, -1);
				st.takeItems(PORTAS_EYE, -1);
				st.takeItems(EXCUROS_SCALE, -1);
				st.takeItems(MORDEOS_TALON, -1);
				st.giveItems(BRAKIS_REMAINS1, 1);
				st.playSound(QuestState.SOUND_MIDDLE);
				return "30630-06.htm";
			}
			if ((st.getQuestItemsCount(BRAKIS_REMAINS1) == 0) && (st.getQuestItemsCount(BRAKIS_REMAINS2) == 0) && (st.getQuestItemsCount(VENDETTA_TOTEM) == 0))
			{
				return "30630-01.htm";
			}
			return "30630-07.htm";
		}
		if (npcId == Pekiron)
		{
			if (st.getQuestItemsCount(PEKIRONS_TOTEM) > 0)
			{
				for (short drop_id : Leto_Lizardman_Drops)
				{
					if (st.getQuestItemsCount(drop_id) == 0)
					{
						return "30682-03.htm";
					}
				}
				st.takeItems(PEKIRONS_TOTEM, -1);
				for (short drop_id : Leto_Lizardman_Drops)
				{
					if (st.getQuestItemsCount(drop_id) == 0)
					{
						st.takeItems(drop_id, -1);
					}
				}
				st.giveItems(TONARS_REMAINS1, 1);
				st.playSound(QuestState.SOUND_MIDDLE);
				return "30682-04.htm";
			}
			if ((st.getQuestItemsCount(TONARS_REMAINS1) == 0) && (st.getQuestItemsCount(TONARS_REMAINS2) == 0) && (st.getQuestItemsCount(VENDETTA_TOTEM) == 0))
			{
				return "30682-01.htm";
			}
			return "30682-05.htm";
		}
		if (npcId == Manakia)
		{
			if (st.getQuestItemsCount(MANAKIAS_TOTEM) > 0)
			{
				if (st.getQuestItemsCount(HERMODTS_SKULL) == 0)
				{
					return "30515-03.htm";
				}
				for (short drop_id : Medusa_Drops)
				{
					if (st.getQuestItemsCount(drop_id) == 0)
					{
						return "30515-03.htm";
					}
				}
				st.takeItems(MANAKIAS_TOTEM, -1);
				st.takeItems(HERMODTS_SKULL, -1);
				for (short drop_id : Medusa_Drops)
				{
					if (st.getQuestItemsCount(drop_id) == 0)
					{
						st.takeItems(drop_id, -1);
					}
				}
				st.giveItems(HERMODTS_REMAINS1, 1);
				st.playSound(QuestState.SOUND_MIDDLE);
				return "30515-04.htm";
			}
			if ((st.getQuestItemsCount(HERMODTS_REMAINS1) == 0) && (st.getQuestItemsCount(HERMODTS_REMAINS2) == 0) && (st.getQuestItemsCount(VENDETTA_TOTEM) == 0))
			{
				return "30515-01.htm";
			}
			if ((st.getQuestItemsCount(RACOYS_TOTEM) == 0) && ((st.getQuestItemsCount(KIRUNAS_REMAINS2) > 0) || (st.getQuestItemsCount(WARSPIRIT_TOTEM) > 0) || (st.getQuestItemsCount(BRAKIS_REMAINS2) > 0) || (st.getQuestItemsCount(HERMODTS_REMAINS2) > 0) || (st.getQuestItemsCount(TAMLIN_ORC_HEAD) > 0) || (st.getQuestItemsCount(TONARS_REMAINS2) > 0)))
			{
				return "30515-05.htm";
			}
		}
		if (npcId == Racoy)
		{
			if (st.getQuestItemsCount(RACOYS_TOTEM) > 0)
			{
				if (st.getQuestItemsCount(INSECT_DIAGRAM_BOOK) == 0)
				{
					return st.getQuestItemsCount(VIVIANTES_LETTER) == 0 ? "30507-03.htm" : "30507-04.htm";
				}
				if (st.getQuestItemsCount(VIVIANTES_LETTER) == 0)
				{
					for (short drop_id : Noble_Ant_Drops)
					{
						if (st.getQuestItemsCount(drop_id) == 0)
						{
							return "30507-05.htm";
						}
					}
					st.takeItems(RACOYS_TOTEM, -1);
					st.takeItems(INSECT_DIAGRAM_BOOK, -1);
					for (short drop_id : Noble_Ant_Drops)
					{
						if (st.getQuestItemsCount(drop_id) == 0)
						{
							st.takeItems(drop_id, -1);
						}
					}
					st.giveItems(KIRUNAS_REMAINS1, 1);
					st.playSound(QuestState.SOUND_MIDDLE);
					return "30507-06.htm";
				}
			}
			else
			{
				if ((st.getQuestItemsCount(KIRUNAS_REMAINS1) == 0) && (st.getQuestItemsCount(KIRUNAS_REMAINS2) == 0) && (st.getQuestItemsCount(VENDETTA_TOTEM) == 0))
				{
					return "30507-01.htm";
				}
				return "30507-07.htm";
			}
		}
		if (npcId == Vivyan)
		{
			if (st.getQuestItemsCount(RACOYS_TOTEM) > 0)
			{
				if (st.getQuestItemsCount(INSECT_DIAGRAM_BOOK) == 0)
				{
					return st.getQuestItemsCount(VIVIANTES_LETTER) == 0 ? "30030-01.htm" : "30030-05.htm";
				}
				if (st.getQuestItemsCount(VIVIANTES_LETTER) == 0)
				{
					return "30030-06.htm";
				}
			}
			else if ((st.getQuestItemsCount(KIRUNAS_REMAINS1) == 0) && (st.getQuestItemsCount(KIRUNAS_REMAINS2) == 0) && (st.getQuestItemsCount(VENDETTA_TOTEM) == 0))
			{
				return "30030-07.htm";
			}
		}
		if (npcId == Sarien)
		{
			if (st.getQuestItemsCount(RACOYS_TOTEM) > 0)
			{
				if ((st.getQuestItemsCount(INSECT_DIAGRAM_BOOK) == 0) && (st.getQuestItemsCount(VIVIANTES_LETTER) > 0))
				{
					st.takeItems(VIVIANTES_LETTER, -1);
					st.giveItems(INSECT_DIAGRAM_BOOK, 1);
					st.playSound(QuestState.SOUND_MIDDLE);
					return "30436-01.htm";
				}
				if ((st.getQuestItemsCount(VIVIANTES_LETTER) == 0) && (st.getQuestItemsCount(INSECT_DIAGRAM_BOOK) > 0))
				{
					return "30436-02.htm";
				}
			}
			else if ((st.getQuestItemsCount(KIRUNAS_REMAINS1) == 0) && (st.getQuestItemsCount(KIRUNAS_REMAINS2) == 0) && (st.getQuestItemsCount(VENDETTA_TOTEM) == 0))
			{
				return "30436-03.htm";
			}
		}
		if ((npcId == Ancestor_Martankus) && (st.getQuestItemsCount(WARSPIRIT_TOTEM) > 0))
		{
			return "30649-01.htm";
		}
		return "noquest";
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState qs = checkPlayerState(player, npc, STATE_STARTED);
		if (qs == null)
		{
			return null;
		}
		int npcId = npc.getNpcId();
		if ((npcId == Porta) && (qs.getQuestItemsCount(ORIMS_CONTRACT) > 0) && (qs.getQuestItemsCount(PORTAS_EYE) < 10))
		{
			qs.giveItems(PORTAS_EYE, 1);
			qs.playSound(qs.getQuestItemsCount(PORTAS_EYE) == 10 ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
		}
		else if ((npcId == Excuro) && (qs.getQuestItemsCount(ORIMS_CONTRACT) > 0) && (qs.getQuestItemsCount(EXCUROS_SCALE) < 10))
		{
			qs.giveItems(EXCUROS_SCALE, 1);
			qs.playSound(qs.getQuestItemsCount(EXCUROS_SCALE) == 10 ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
		}
		else if ((npcId == Mordeo) && (qs.getQuestItemsCount(ORIMS_CONTRACT) > 0) && (qs.getQuestItemsCount(MORDEOS_TALON) < 10))
		{
			qs.giveItems(MORDEOS_TALON, 1);
			qs.playSound(qs.getQuestItemsCount(MORDEOS_TALON) == 10 ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
		}
		else if (((npcId == Noble_Ant) || (npcId == Noble_Ant_Leader)) && (qs.getQuestItemsCount(RACOYS_TOTEM) > 0))
		{
			FastList<Integer> drops = new FastList<>();
			for (short drop_id : Noble_Ant_Drops)
			{
				if (qs.getQuestItemsCount(drop_id) == 0)
				{
					drops.add((int) drop_id);
				}
			}
			if ((drops.size() > 0) && Rnd.chance(30))
			{
				int drop_id = drops.get(Rnd.get(drops.size()));
				qs.giveItems(drop_id, 1);
				qs.playSound(drops.size() == 1 ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
			}
			drops.clear();
			drops = null;
		}
		else if (((npcId == Leto_Lizardman_Shaman) || (npcId == Leto_Lizardman_Overlord)) && (qs.getQuestItemsCount(PEKIRONS_TOTEM) > 0))
		{
			FastList<Integer> drops = new FastList<>();
			for (short drop_id : Leto_Lizardman_Drops)
			{
				if (qs.getQuestItemsCount(drop_id) == 0)
				{
					drops.add((int) drop_id);
				}
			}
			if ((drops.size() > 0) && Rnd.chance(25))
			{
				int drop_id = drops.get(Rnd.get(drops.size()));
				qs.giveItems(drop_id, 1);
				qs.playSound(drops.size() == 1 ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
			}
			drops.clear();
			drops = null;
		}
		else if ((npcId == Medusa) && (qs.getQuestItemsCount(MANAKIAS_TOTEM) > 0))
		{
			FastList<Integer> drops = new FastList<>();
			for (short drop_id : Medusa_Drops)
			{
				if (qs.getQuestItemsCount(drop_id) == 0)
				{
					drops.add((int) drop_id);
				}
			}
			if ((drops.size() > 0) && Rnd.chance(30))
			{
				int drop_id = drops.get(Rnd.get(drops.size()));
				qs.giveItems(drop_id, 1);
				qs.playSound((drops.size() == 1) && (qs.getQuestItemsCount(HERMODTS_SKULL) > 0) ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
			}
			drops.clear();
			drops = null;
		}
		else if ((npcId == Stenoa_Gorgon_Queen) && (qs.getQuestItemsCount(MANAKIAS_TOTEM) > 0) && (qs.getQuestItemsCount(HERMODTS_SKULL) == 0) && Rnd.chance(30))
		{
			qs.giveItems(HERMODTS_SKULL, 1);
			boolean _allset = true;
			for (short drop_id : Medusa_Drops)
			{
				if (qs.getQuestItemsCount(drop_id) == 0)
				{
					_allset = false;
					break;
				}
			}
			qs.playSound(_allset ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
		}
		else if (((npcId == Tamlin_Orc) || (npcId == Tamlin_Orc_Archer)) && (qs.getQuestItemsCount(VENDETTA_TOTEM) > 0) && (qs.getQuestItemsCount(TAMLIN_ORC_HEAD) < 13))
		{
			if (Rnd.chance(npcId == Tamlin_Orc ? 30 : 50))
			{
				qs.giveItems(TAMLIN_ORC_HEAD, 1);
				qs.playSound(qs.getQuestItemsCount(TAMLIN_ORC_HEAD) == 13 ? QuestState.SOUND_MIDDLE : QuestState.SOUND_ITEMGET);
			}
		}
		return null;
	}
	
	public static void onLoad()
	{
		new Q233_TestOfWarspirit(233, qn, "Test Of Warspirit");
	}
}
